package com.dao;


import com.DBM.DBManager;
import com.entity.TasteDto;

import java.sql.*;

import java.util.ArrayList;
import java.util.List;

public class IceDao extends DBManager{
    public List<TasteDto> LoadAll(int page, int displaycount) throws SQLException {
        List<TasteDto> DtoList = new ArrayList<TasteDto>();
        String sql = "\tSELECT * FROM goods \n" +
                "\tLEFT JOIN goodsattribute\n" +
                "\tON goods.goodsId=goodsAttribute.goodsAttributeId\n" +
                "\tLEFT JOIN photo\n" +
                "\tON goods.goodsId=photo.goodsId\n" +
                "\tLEFT JOIN type\n" +
                "\tON goods.goodsId=type.goodsId\n" +
                "\tWHERE photo.photoId%2=0 AND type.typeName=\"冰淇淋味\"\n limit ?,? ";
        Connection conn = this.openconnection();
        Object object[] = {(page-1)*displaycount,displaycount};
        ResultSet rs = this.query(conn,sql,object);
        System.out.println("进入了Dao");
        while (rs.next()){
            TasteDto c = new TasteDto();
            c.setGoodsId(rs.getInt("goodsId"));
            c.setPhotoId(rs.getInt("photoId"));
            c.setPhotoUrl(rs.getString("photoUrl"));
            c.setGoodsName(rs.getString("goodsName"));
            c.setGoodsInfo(rs.getString("goodsInfo"));
            c.setDisCount(rs.getInt("discount"));
            c.setPrice(rs.getDouble("price"));
            c.setGoodsSize(rs.getInt("goodsSize"));
            c.setTypeId(rs.getInt("typeId"));
            c.setTypeName(rs.getString("typeName"));
            DtoList.add(c);
        }
        System.out.println("c对象内的值是否为空"+DtoList.size());
        this.closeconnection(conn);
        return DtoList;
    }
    public int getCount() throws SQLException{
        int Count = 0;
        String sql = "SELECT Count(*) FROM goods";
        Connection conn = this.openconnection();
        ResultSet rs = this.query(conn,sql,null);
        if(rs.next()){
            Count = rs.getInt(1);
        }
        this.closeconnection(conn);
        return Count;
    }
}